home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/dup,v $
- * $Date: 1996/10/30 21:59:01 $
- * $Revision: 1.2 $
- * $State: Rel $
- * $Author: unixlib $
- *
- * $Log: dup,v $
- * Revision 1.2 1996/10/30 21:59:01 unixlib
- * Massive changes made by Nick Burret and Peter Burwood.
- *
- * Revision 1.1 1996/04/19 21:35:27 simon
- * Initial revision
- *
- ***************************************************************************/
-
- static const char rcs_id[] = "$Id: dup,v 1.2 1996/10/30 21:59:01 unixlib Rel $";
-
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
-
- #include <sys/types.h>
- #include <sys/unix.h>
- #include <sys/os.h>
-
- int
- dup (int fd)
- {
- register struct file *f1, *f2;
- register int i;
-
- if (BADF (fd))
- {
- errno = EBADF;
- return (-1);
- }
-
- if ((i = __fdalloc ()) < 0)
- return (-1);
-
- f1 = __u->file + i;
- f2 = __u->file + fd;
-
- memcpy (f1, f2, sizeof (struct file));
-
- f1->dup = f2;
-
- while (f2->dup != f1->dup)
- f2 = f2->dup;
-
- f2->dup = f1;
-
- return (i);
- }
-